import LayVmodelBase from "./LayVmodelBase.js"
import { logger,tools,runtime } from "./tools.js";


/**Layui单日期 */
class LayDateVmodel extends LayVmodelBase {
    /**LayDate 配置 */
    config = null;
    layDate = null;

    constructor(elem,config) {
        super(elem);
        this.config = config;    
    }

    /**[override] */
    viewModelInit() {
        super.viewModelInit();
        let vm = this.vm;
        vm.value = ko.observable();
        vm.begin = ko.observable();
        vm.end = ko.observable();
    }

    /**[override] */
    init() {
        if(!super.init()) return;
        if (!this.config) {
            this.config = {
                type: "date",
                range: false,
                format: "yyyy-MM-dd",
                done: null
            };
        }        
        var tis = this;
        let vm = this.vm;
        var config = this.config;
        config.elem = this.element;
        config.range = false;
        
        let innerDone = config.done;
        config.done = function (value, date, endDate) {
            vm.value(value);
            innerDone && innerDone.call(this, ...arguments);
        }
        let laydate = tools.laymod("laydate");
        // 日期
        laydate.render(config);

        //订阅value变化
        vm.value.subscribe((newValue) => {
            $(this.element).val(newValue);
        });
        $(this.element).val(vm.value());

        this.inited = true;
    }
}

export default LayDateVmodel;